<?php
class indexController extends baseController{
	
	//首页
	public function index(){
		$this->getCallbackUrl();
		$this->recommendList = model('appstore')->select(); //推荐应用
		$this->newList = model('appstore')->select(); //最新应用
		$this->right();
		$this->display();
	}
	
	//详情页面
	public function detail(){
		$this->info = model('appstore')->find( array( 'id'=>intval($_GET['id']) ) );
		if( false == $this->info ){
			$this->alert('参数传递错误！');
		}
		$this->right();
		$this->display();
	}
	
	//获取下载地址
	public function getDownloadUrl(){
		$result = array('code'=>0, 'url'=>'');
		if(isset( $_COOKIE['appstore_callback'] ) && check::url($_COOKIE['appstore_callback']) ){
			$callback = $_COOKIE['appstore_callback'];
			$callback = $callback . ((false === strpos($callback, '?')) ? '?' : '&');
			$url = url('index/download', array('id'=>intval($_GET['id'])) );
			if( false === stripos($url, 'http://') ){
				$url = 'http://' . $_SERVER['HTTP_HOST'] . $url;
			}
			$result = array('code'=>1, 'url'=>$callback.'url='.urlencode($url) );
		}
		echo json_encode($result);
	}
	
	//下载页面
	public function download(){
		$result = array('status'=>0, 'url'=>'');
		$result['url'] = 'http://www.canphp.com';
		echo json_encode($result);
	}

	protected function right(){
		$this->moreList = model('appstore')->select(); //安装最多
	}
	
	protected function getCallbackUrl(){
		if( empty($_GET['domain']) || empty($_GET['callback']) ){
		//	$this->alert('参数错误！');
		}
		$domain = $_GET['domain'];
		$callback = $_GET['callback'];
		if( false === stripos($callback, 'http://') ){
			$callback = 'http://' . $domain . $callback;
		}
		setcookie('appstore_callback', $callback);
		return $callback;
	}
}